import { Component } from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';

//引入请求数据的模块
import { Http, Jsonp} from '@angular/http';

//新闻详情
import { NewscontentPage } from '../newscontent/newscontent';

/**
 * Generated class for the NewsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-news',
  templateUrl: 'news.html',
})
export class NewsPage {

  //list用于存放数据

  public list=[];

  public page=1;
  public NewscontentPage=NewscontentPage;   /*新闻详情页面*/

  constructor(public navCtrl: NavController,public http: Http,public jsonp: Jsonp) {
     
      this.requestData('');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad NewsPage');
    
  }
  requestData(infiniteScroll){  /*请求数据*/
    var that=this;
    var url='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page='+this.page;
    
    this.http.get(url).subscribe(function(data){
      // console.log(data['_body']);

      var response=JSON.parse(data['_body']).result;
      
      that.list=that.list.concat(response);   /*拼接每次请求的数据*/

      that.page++;   /*每次请求完成page+1 */

      if(infiniteScroll){
        infiniteScroll.complete();   /*请求完成数据更新页面*/
        if(response<20){   /*没有数据 禁用上拉更新*/
          infiniteScroll.enable(false); 
        }
      }
        console.log('上拉加载成功' + that.page);

    },function(err){

    })

  }
  /*上拉更新*/
  doInfinite(infiniteScroll){  /*接收事件对象传值*/
      // console.log('11111')
      this.requestData(infiniteScroll);     

  }



}
